﻿using Microsoft.EntityFrameworkCore;
using Simple.EntityFramewokCore;
using Video.Domain;
using Video.Domain.Users;
using Video.Domain.Videos;


namespace Video.EntityFrameworkCore;

public class VideoDbContext : MasterDbContext<VideoDbContext>
{
    public DbSet<UserInfo> UserInfo { get; set; }
    public DbSet<Role> Role { get; set; }
    public DbSet<UserRole> UserRole { get; set; }
    public DbSet<Domain.Videos.Video> Videos { get; set; }
    public DbSet<Likes> Like { get; set; }
    public DbSet<Comment> Comment { get; set; }
    public DbSet<Classify> Classify { get; set; }

    public DbSet<BeanVermicelli> BeanVermicelli { get; set; }


    public VideoDbContext(DbContextOptions<VideoDbContext> options) : base(options)
    {

    }


    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<UserInfo>(u =>
        {
            u.ToTable("UserInfos");
            u.HasComment("用户表");

            u.HasKey(x => x.Id);
            u.HasIndex(x => x.Id);

            u.Property(x => x.UserName).HasComment("用户名");
            u.HasIndex(x => x.UserName).IsUnique();
        });

        builder.Entity<Role>(x =>
        {
            x.ToTable("Roles");
            x.HasComment("角色表");

            x.HasKey(x => x.Id);
            x.HasIndex(x => x.Id);
        });

        builder.Entity<UserRole>(x =>
        {
            x.ToTable("UserRoles");
            x.HasComment("用户角色配置表");

            x.HasKey(x => x.Id);
            x.HasIndex(x => x.Id);
            x.HasIndex(x => x.UserId);
            x.HasIndex(x => x.RoleId);
        });

        builder.Entity<Domain.Videos.Video>(x =>
        {
            x.ToTable("Videos");
            x.HasComment("视频表");

            x.HasKey(x => x.Id);
            x.HasIndex(x => x.Id);
            x.HasIndex(x => x.ClassifyId);
            x.HasIndex(x => x.UserId);
        });


        builder.Entity<Likes>(x =>
        {
            x.ToTable("Likes");
            x.HasComment("视频点赞表");

            x.HasKey(x => x.Id);
            x.HasIndex(x => x.Id);
            x.HasIndex(x => x.VideoId);
            x.HasIndex(x => x.UserId);
        });


        builder.Entity<Comment>(x =>
        {
            x.ToTable("Comments");
            x.HasComment("评论表");

            x.HasKey(x => x.Id);
            x.HasIndex(x => x.Id);
            x.HasIndex(x => x.VideoId);
            x.HasIndex(x => x.ParentId);
            x.HasIndex(x => x.UserId);
        });


        builder.Entity<Classify>(x =>
        {
            x.ToTable("Classifys");
            x.HasComment("视频分类表");

            x.HasKey(x => x.Id);
            x.HasIndex(x => x.Id);
        });

        builder.Entity<BeanVermicelli>(x =>
        {
            x.ToTable("BeanVermicellis");
            x.HasComment("关注表");

            x.HasKey(x => x.Id);
            x.HasIndex(x => x.Id);
            x.HasIndex(x => x.UserId);
            x.HasIndex(x => x.BeFocusedId);
        });

        #region 初始化数据
        var userInfo = new UserInfo()
        {
            Id = new Guid(),
            Avatart = "",
            CreatedTime = DateTime.Now,
            Enable = true,
            Name = "admin",
            UserName = "admin",
            Password = "admin",
        };

        var role = new Role()
        {
            Id = Guid.NewGuid(),
            Name = "admin",
            Code = "admin",
        };

        var userRole = new UserRole()
        {
            Id = Guid.NewGuid(),
            RoleId = role.Id,
            UserId = userInfo.Id,
        };

        #endregion

        builder.Entity<UserInfo>().HasData(userInfo);
        builder.Entity<Role>().HasData(role);
        builder.Entity<UserRole>().HasData(UserRole);
    }

}


